﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace System {
    public static partial class Extention {
        public static string BuilderSelectBox(IDictionary<int,string> options,string id,bool includeUnknown) {
            var html = new StringBuilder();
            html.AppendFormat("<select id=\"{0}\" name=\"{0}\">",id);
            html.AppendLine();
            if(includeUnknown) {
                html.AppendLine("\t<option>Unknown</option>");
            }
            foreach(var opt in options) {
                html.AppendFormat("\t<option value=\"{0}\">{1}</option>",opt.Key,opt.Value);
                html.AppendLine();
            }
            html.AppendLine("</select>");
            return html.ToString();
        }
        public static void AppendCollection<TItem>(this StringBuilder builder,IEnumerable<TItem> items,Func<TItem,string> valueSelector) {
            foreach(TItem item in items) {
                builder.Append(valueSelector(item));
            }
        }
        public static void AppendCollection<TItem>(this StringBuilder builder,IEnumerable<TItem> items) {
            AppendCollection(builder,items,x => x.ToString());
        }
        /// <summary>
        /// 检索value最近出现的位置
        /// </summary>
        /// <param name="sb">StringBuilder字符串类型</param>
        /// <param name="value">要检索的值</param>
        /// <returns></returns>
        public static int IndexOf(this StringBuilder sb,char value) {
            for(int i = 0;i<sb.Length;i++) {
                if(sb[i]==value)
                    return i;
            }
            return -1;
        }
        /// <summary>
        /// Reverse 逆序扩展
        /// </summary>
        /// <param name="builder"></param>
        public static void Reverse(this StringBuilder builder) {
            var result = new StringBuilder();
            for(var i = builder.Length - 1;i >= 0;i--) {
                result.Append(builder[i]);
            }
            builder.Remove(0,builder.Length);
            builder.Append(result);
        }

        #region 删除字符
        /// <summary>
        /// 删除最后结尾的指定字符后的字符 需要重新赋值 比如：
        ///  strSql=strSql.DelLastComma(); strSql.Append(";");
        /// </summary>
        public static StringBuilder DelLastComma(this StringBuilder builder,string strchar= ",") {
            string strtemp = builder.ToString();
            int n = strtemp.LastIndexOf(strchar);
            if(n > 0) {
                builder = new StringBuilder();
                builder.Append(strtemp.Substring(0,n));
            }
            return builder;
        }
        /// <summary>
        /// 删除指定位置的字符
        /// </summary>
        /// <param name="Start">开始索引</param>
        /// <param name="Num">删除个数</param>
        public static void Remove(this StringBuilder builder,int Start,int Num) {
            builder.Remove(Start,Num);
        }
        public static void Clear(this StringBuilder builder) {
            builder.Remove(0,builder.Length);
        }
        #endregion
    }
}